﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
// using System.Web.Routing;
using CodeDDD.Infrastructure.Crosscutting.Seedwork.IoC;

namespace CodeDDD.Presentation.MVC
{
    /// <summary>
    /// Custom controller factory for ASP.NET MVC. In this controller
    /// factory, controller instances are instantiated using an IoC container
    /// </summary>
    public class IoCControllerFactory : DefaultControllerFactory
    {
        #region Members

        private IContainer _container;

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="container"></param>
        public IoCControllerFactory(IContainer container)
        {
            if (container == (IContainer)null)
                throw new ArgumentNullException("container");

            _container = container;
        }

        #endregion

        #region DefaultControllerFactory Overrides

        /// <summary>
        /// This method create a new instance of a controller using an IoC container in order to handle a users request.
        /// </summary>
        /// <param name="requestContext">The context of the current HTTP request.</param>
        /// <param name="controllerType">The type of the controller that handles the request.</param>
        /// <returns>An instance of the controller.</returns>
        //protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        //{
        //    if (controllerType != (Type)null)
        //    {
        //        // Try to resolve the controller using asociated IoC container.
        //        return _container.Resolve(controllerType) as IController;
        //    }
        //    else
        //    {
        //        // Fallback to default behavior.
        //        return base.GetControllerInstance(requestContext, controllerType);
        //    }
        //}

        #endregion
    }
}
